﻿using System;
using System.Collections;
using System.Windows;
using System.Windows.Data;
using ExportToExcelTools;
using Project5188.BUS;
using Project5188.BUS.DocumentManagement;
using Project5188.Client.ClientCodes;

namespace Project5188.Client.DocumentManagement
{
    /// <summary>
    /// Interaction logic for DocumentFlowPage.xaml
    /// </summary>
    public partial class DocumentFlowPage
    {
        private CollectionViewSource _resources;

        public DocumentFlowPage()
            : base(Extensions.PageNames.DocumentPage)
        {
            InitializeComponent();

            pagingControl.DataChanged += PagingControlDataChanged;

            BindData();
        }

        private void BindData()
        {
            var parameterValues = new ParameterValues();

            drpFlowTypes.SelectedValuePath = "ID";
            drpFlowTypes.IsSynchronizedWithCurrentItem = true;
            drpFlowTypes.ItemTemplate = Resources["flowTypesTemplate"] as DataTemplate;
            drpFlowTypes.ItemsSource = parameterValues.GetEntityByParameterId(31);
        }

        void PagingControlDataChanged(object sender, EventArgs e)
        {
            if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
            if (_resources != null) dgDocumentApprovals.ItemsSource = _resources.Source as IEnumerable;
        }

        //private void BtnAddClick(object sender, RoutedEventArgs e)
        //{
        //    var insert = new InsertWindow();
        //    insert.Closed += InsertClosed;
        //    DetailView<documentflow> documentFlowDetail = new DocumentFlow();
        //    insert.SetEntity(documentFlowDetail, Extensions.PageNames.DocumentFlowPage.ToString());
        //    insert.Show();
        //}

        public override bool Search()
        {
            if (base.Search())
            {
                var documentFlows = new DocumentFlow();
                _resources = Resources["documentApprovalsViewSource"] as CollectionViewSource;
                pagingControl.SetDataSource(
                    documentFlows.GetEntityListByParameters(Convert.ToInt64(drpFlowTypes.SelectedValue), UserInfo.UserId), 1, 10);
                if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
                if (_resources != null) dgDocumentApprovals.ItemsSource = _resources.Source as IEnumerable;
            }
            else if (TransactionInfo.TransactionId.HasValue)
            {
                var documentFlows = new DocumentFlow();
                _resources = Resources["documentApprovalsViewSource"] as CollectionViewSource;
                pagingControl.SetDataSource(
                    documentFlows.GetEntityById(TransactionInfo.TransactionId.Value), 1, 10);
                if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
                if (_resources != null) dgDocumentApprovals.ItemsSource = _resources.Source as IEnumerable;
            }

            return true;
        }

        private void BtnDetailClick(object sender, RoutedEventArgs e)
        {
            var documentFlowValue = (documentflow)dgDocumentApprovals.SelectedItem;
            var detail = new DetailWindow();
            DetailView<documentflow> documentFlowValueDetail = new DocumentFlow { Entity = documentFlowValue };
            detail.SetEntity(documentFlowValueDetail, Extensions.PageNames.DocumentFlowPage.ToString());
            detail.Show();
        }

        private void BtnUpdateClick(object sender, RoutedEventArgs e)
        {
            var documentFlowValue = (documentflow) dgDocumentApprovals.SelectedItem;

            var update = new UpdateWindow();
            update.Closed += UpdateClosed;
            DetailView<documentflow> documentFlowValueDetail = new DocumentFlow();
            update.SetEntity(documentFlowValueDetail, Extensions.PageNames.DocumentFlowPage.ToString(), documentFlowValue.a_ID);
            update.Show();
        }

        //private void BtnDeleteClick(object sender, RoutedEventArgs e)
        //{
        //    var documentFlowValue = (documentflow)dgDocumentApprovals.SelectedItem;
        //    DetailView<documentflow> documentFlowValueDetail = new DocumentFlow();
        //    GenerateDeleteDialog(documentFlowValueDetail, documentFlowValue.a_ID);
        //}

        private void BtnPrintClick(object sender, RoutedEventArgs e)
        {
            try
            {
                dgDocumentApprovals.ExportToExcel();
            }
            catch
            {
                MessageBox.Show("Excel'e aktarma sırasında hata oluştu.");
            }
        }
    }
}
